package com.yyfax.ph.main;

import android.content.Context;
import android.text.TextUtils;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.ph.Global;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.common.GlobalVariableConfig;
import com.yyfax.ph.common.utils.preferenceutils.PreferenceManager;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 获取首页广告业务逻辑 以及获取成功之后的逻辑处理
 */
public class MainPresenter extends Presenter<MainModel, MainPresenter.View> {
	private static final String TAG = MainPresenter.class.getSimpleName();

	public static class Generator implements PresenterFactory<MainPresenter> {
		@Override
		public MainPresenter create() {
			return new MainPresenter();
		}
	}

	public interface View extends IView {
		void onGetNotice(MainModel.NoticeBean noticeBean);

		void onGetNoticeFailure();
	}

	private View mView;

	private PreferenceManager mPreferencesManager;

	@Override
	public void attach(View view) {
		mView = view;
		mModel = new MainModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public void setupPreferencesManager(Context ctx) {
		mPreferencesManager = YJPH.getInstance().getPreferenceManager();
	}

	public void getRsaKey() {
		if (TextUtils.isEmpty(GlobalVariableConfig.getKeyRSA())) {
			execute(null, MainModel.MSG_GET_RSA);
		}
	}

	public void getNotice() {
		execute(null, MainModel.MSG_GET_NOTICE);
	}

	public void getConfig() {
		execute(null, MainModel.APP_CONFIG);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void rsaResult(MainModel.RSAResult event) {
		if (isResponseSuccess(event)) {
			String rsa = event.getContent();
			GlobalVariableConfig.setKeyRSA(rsa);
			CLog.i(TAG, "RSA=" + rsa);
		}
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void noticeResult(MainModel.NoticeResult event) {
		if (isResponseSuccess(event)) {
			MainModel.NoticeBean bean = event.getContent();
			CLog.i(TAG, "rs=" + bean.toString());
			if (TextUtils.isEmpty(bean.id)) {
				mView.onGetNoticeFailure();
				return;
			}
			if (TextUtils.isEmpty(bean.content) && TextUtils.isEmpty(bean.title)) {
				mView.onGetNoticeFailure();
				return;
			}

			if (null != mPreferencesManager && !TextUtils.equals(bean.id, mPreferencesManager.getLastShowedNoticeId())) {
				mView.onGetNotice(bean);
			} else {
				mView.onGetNoticeFailure();//没有应该弹出的公告也要回调 供检查推荐中断
			}
		} else {
			mView.onGetNoticeFailure();
		}
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void configResult(MainModel.ConfigResult result) {
		CLog.i(TAG, "rs=" + result.toString());
		if (isResponseSuccess(result)) {
			MainModel.Config config = result.getContent();
			MainModel.Compression compress = config.compression;
			try {
				Global.IMAGE_QUALITY = Integer.parseInt(compress.quality);
			} catch (Exception e) {
				Global.IMAGE_QUALITY = 90;
			}
			Global.IMAGE_RESIZE = compress.resize;
			Global.IMAGE_MIN_SIZE = compress.minSize;
			Global.IMAGE_WIDTH = compress.width;
			Global.IMAGE_HEIGHT = compress.height;
		}
	}
}
